package cn.edu.sdjzu.xg.eagle.Test;

import cn.edu.sdjzu.xg.eagle.Service.DepartmentDaoService;
import cn.edu.sdjzu.xg.eagle.Service.SchoolDaoService;
import cn.edu.sdjzu.xg.eagle.dao.DepartmentDaoImpl;
import cn.edu.sdjzu.xg.eagle.dao.SchoolDaoImpl;
import cn.edu.sdjzu.xg.eagle.entity.Department;
import cn.edu.sdjzu.xg.eagle.entity.School;
import cn.edu.sdjzu.xg.eagle.util.JdbcHelper;

import java.sql.Connection;
import java.sql.SQLException;


public class SchoolDaoTest {
    public static void main(String[] args) throws SQLException {

        SchoolDaoService schoolDao = SchoolDaoImpl.getInstance();
        DepartmentDaoService departmentDao = DepartmentDaoImpl.getInstance();

        Connection connection = null;

        try {
            connection = JdbcHelper.getConn();
            connection.setAutoCommit(false);

            System.out.println(schoolDao.findSchools());

            System.out.println(departmentDao.findDepartments());

//            //新加学院
//            School school = new School("艺术学院","S003","美术、舞蹈、音乐");
//            //新加学院状态
//            boolean isSchoolAdd = schoolDao.addSchool(connection, school);
//            System.out.println("添加状态:" + isSchoolAdd);
//            //新加专业
//            Department department01 = new Department("美术","#005","油画，水彩等",school);
//            Department department02 = new Department("音乐","#006","古典，摇滚等",school);
//
//            boolean isDepartmentAdd01 = departmentDao.addDepartment(connection, department01);
//            boolean isDepartmentAdd02 = departmentDao.addDepartment(connection, department02);
//
//            System.out.println("添加department1状态" + isDepartmentAdd01);
//            System.out.println("添加department2状态" + isDepartmentAdd02);
//
//            System.out.println(schoolDao.findSchools());
//            System.out.println(departmentDao.findDepartments());
//
//            //测试更新
//            department02.setDescription("音乐(现代)");
//            departmentDao.updateDepartment(connection, department02);

//            //测试删除
//            boolean isDelete = departmentDao.delete(department01.getId());
//            System.out.println("删除Department后的结果:" + isDelete);
//
            connection.commit();
            System.out.println("事物提交成功");

        }catch (Exception e) {
            e.printStackTrace();
            try {
                if (connection != null) {
                    connection.rollback();
                }
            }catch (SQLException e1) {
                e1.printStackTrace();
            }finally {
                JdbcHelper.close(connection);
            }
        }
    }
}